package com.tct.worktimeboard.service.impl.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tct.worktimeboard.entity.WorkGroup;
import com.tct.worktimeboard.entity.WorkGroupVO;
import com.tct.worktimeboard.mapper.WorkGroupMapper;
import com.tct.worktimeboard.service.IWorkGroupService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author Huhailong
 * @Description
 * @Date 2021/4/24.
 */
@Slf4j
public class PowerListener extends AnalysisEventListener<Map<Integer,String>> {

    private List<Map<Integer,String>> list = new ArrayList<>();

    IWorkGroupService workGroupService;

    public PowerListener(IWorkGroupService workGroupService){
        this.workGroupService = workGroupService;
    }

    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext context) {
        // TODO Auto-generated method stub
//		System.out.println(data);
        list.add(data);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        Function<Map<Integer,String>,String> s = t -> {
            // TODO Auto-generated method stub
            String groupName = t.get(1);
            return groupName;
        };
        Map<String,List<Map<Integer,String>>> collect = list.stream().collect(Collectors.groupingBy(s));
        System.out.println(JSONObject.toJSON(collect));
        int len = collect.size();
        Set<Map.Entry<String, List<Map<Integer,String>>>> set = collect.entrySet();
        List<Map<String,Object>> param = new ArrayList<>();
        for(Map.Entry<String, List<Map<Integer,String>>> map : set) {
            Map<String,Object> temp = new HashedMap<>();
            Map<String,Object> workGroup = new HashedMap<>();
            workGroup.put("groupName", map.getKey());
            temp.put("powerUserIdList", map.getValue().get(0).get(5));
            workGroup.put("groupTypeId", Long.parseLong(map.getValue().get(0).get(2)));
            List<Map<Integer,String>> tempUserList = map.getValue();
            List<Long> userList = new ArrayList<>();
            for(Map<Integer,String> t : tempUserList) {
                userList.add(Long.valueOf(t.get(4)));
            }
            temp.put("userIdList", JSONObject.toJSONString(userList));
            workGroup.put("createUsername", "hailong.hu");
            workGroup.put("groupTypeCount", tempUserList.size());
            workGroup.put("createUserId", 0);
            temp.put("workGroup", workGroup);
            param.add(temp);
        }
        for (Map<String, Object> stringObjectMap : param) {
            WorkGroup group = new WorkGroup();
            WorkGroupVO vo = new WorkGroupVO();
            Map<String, Object> temp = (Map<String, Object>) stringObjectMap.get("workGroup");
            group.setCreateUserId(0L);
            group.setGroupCount((Integer) temp.get("groupTypeCount"));
            group.setGroupName((String) temp.get("groupName"));
            group.setGroupTypeId(Long.valueOf(String.valueOf(temp.get("groupTypeId"))).longValue());
            vo.setWorkGroup(group);
            log.info("{}", param.get(0).get("userIdList:"));
            vo.setPowerUserIdList(JSONArray.parseArray((String) stringObjectMap.get("powerUserIdList")).toJavaList(Long.class));
            vo.setUserIdList(JSONArray.parseArray((String) stringObjectMap.get("userIdList")).toJavaList(Long.class));
            workGroupService.createGroup(vo);
        }
    }
}
